﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Weight.Base
{
    public class AppContainer
    {
        public const int SWP_NOOWNERZORDER = 512;

        public const int SWP_NOREDRAW = 8;

        public const int SWP_NOZORDER = 4;

        public const int SWP_SHOWWINDOW = 64;

        public const int WS_EX_MDICHILD = 64;

        public const int SWP_FRAMECHANGED = 32;

        public const int SWP_NOACTIVATE = 16;

        public const int SWP_ASYNCWINDOWPOS = 16384;

        public const int SWP_NOMOVE = 2;

        public const int SWP_NOSIZE = 1;

        public const int GWL_STYLE = -16;

        public const int WS_VISIBLE = 268435456;

        public const int WM_CLOSE = 16;

        public const int WS_CHILD = 1073741824;

        public const int SW_HIDE = 0;

        public const int SW_SHOWNORMAL = 1;

        public const int SW_NORMAL = 1;

        public const int SW_SHOWMINIMIZED = 2;

        public const int SW_SHOWMAXIMIZED = 3;

        public const int SW_MAXIMIZE = 3;

        public const int SW_SHOWNOACTIVATE = 4;

        public const int SW_SHOW = 5;

        public const int SW_MINIMIZE = 6;

        public const int SW_SHOWMINNOACTIVE = 7;

        public const int SW_SHOWNA = 8;

        public const int SW_RESTORE = 9;

        public const int SW_SHOWDEFAULT = 10;

        public const int SW_MAX = 10;

        public const int HWND_TOP = 0;

        public const int WM_COMMAND = 274;

        public const int WM_QT_PAINT = 49884;

        public const int WM_PAINT = 1;

        public const int WM_SIZE = 1;

        public const uint GW_HWNDFIRST = 0u;

        public const uint GW_HWNDLAST = 1u;

        public const uint GW_HWNDNEXT = 2u;

        public const uint GW_HWNDPREV = 3u;

        public const uint GW_OWNER = 4u;

        public const uint GW_CHILD = 5u;

        public uint GW_ENABLEDPOPUP = 6u;

        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
        public static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
        public static extern long GetWindowLong(IntPtr hwnd, int nIndex);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

        [DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "SetWindowLong")]
        public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "SetWindowLongPtr")]
        public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

        [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
        public static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetParent(IntPtr hwnd);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        [SecuritySafeCritical]
        internal static void MoveWindow(Process app, Control control)
        {
            if (app != null)
            {
                AppContainer.MoveWindow(app.MainWindowHandle, 0, 0, control.Width, control.Height, true);
            }
        }

        [SecuritySafeCritical]
        internal static void SetWindowLong(HandleRef handleRef)
        {
            AppContainer.SetWindowLong(handleRef, -16, 268435456);
        }

        [SecuritySafeCritical]
        internal static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
        {
            if (IntPtr.Size == 4)
            {
                return AppContainer.SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
            }
            return AppContainer.SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
        }
    }
}
